# -*- coding: utf-8 -*-
"""
Created on Tue Oct 22 15:10:34 2019

@author: jm
"""
import requests
import pandas as pd

app_id = 'f1a38b71'
app_key = '46651f612db0406771ccfd34c7431dee'

params = {'app_id':app_id,
          'app_key':app_key,
        }

baseurl = 'https://api.data.charitynavigator.org/v2'
data = pd.read_csv('eins.csv', dtype=str)

#%%
scores = []
for i, (name, ein) in enumerate(zip(data['name'],data['ein'])):
    api = f'/Organizations/{ein}/Ratings'
    url = f'{baseurl}{api}'
    R = requests.get(url, params=params)
    R.raise_for_status()
    
    responses = R.json()
    for response in responses:
        scores.append((name, ein,response['publicationDate'],response['score'],response['ratingID']))
        print(name, response['publicationDate'], response['score'], response['ratingID'])
    if i > 3:
        break
    

#%%
data = pd.DataFrame(scores, columns=['name','ein','publicationDate','score','ratingID'])
data.to_csv('scores.csv', index=False)

